package com.jweb.framework.ws.soap.axiom;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Result;
import javax.xml.transform.Source;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMException;
import org.apache.axiom.soap.SOAPBody;
import org.apache.axiom.soap.SOAPFactory;
import org.springframework.util.Assert;
import org.springframework.xml.transform.StaxSource;
/**
 * 修改人： Thomas
 * 修改内容：新增 
 * 类说明：
 */
abstract class Payload {
	
	private final SOAPBody axiomBody ;
	
	private final SOAPFactory axiomFactory ;
	
	protected Payload(SOAPBody axiomBody , SOAPFactory axiomFactory) {
		Assert.notNull(axiomBody, "axiomBody must not be null");
		Assert.notNull(axiomFactory, "axiomFactory must not be null");
		this.axiomBody = axiomBody ;
		this.axiomFactory = axiomFactory ;
	}
	
	public final Source getSource(){
		try {
			OMElement payloadElement = getPayloadElement(); // <p0:queryAcctDate xmlns:p0="http://gsp.service.bankcomm.com/abstraction/atomic/technical.QueryAcctDateService/1_0" xmlns:p1="http://gsp.service.bankcomm.com/QueryAcctDateService"><p1:version>1.0</p1:version><p1:TIA><p1:TIA_CICS>AG63</p1:TIA_CICS><p1:TIA_ICS>458990</p1:TIA_ICS><p1:TIA_TRCODE>458990</p1:TIA_TRCODE><p1:TIA_TLID>EIRIBB1</p1:TIA_TLID><p1:TIA_AUTH>00</p1:TIA_AUTH><p1:TIA_TRBR>310800</p1:TIA_TRBR></p1:TIA></p0:queryAcctDate>
			if(payloadElement != null){
				XMLStreamReader streamReader = getStreamReader(payloadElement);
				return new StaxSource(streamReader);
			}
			
			return null ;
		} catch (OMException e) {
			throw new AxiomSoapBodyException(e);
		}
	}
	
	protected abstract XMLStreamReader getStreamReader(OMElement payloadElement);
	
	public final Result getResult(){
		AxiomUtils.removeContents(getAxiomBody());
		return getResultInternal();
	}
	
	protected abstract Result getResultInternal();
	
	public SOAPFactory getAxiomFactory(){
		return this.axiomFactory ;
	}
	
	protected SOAPBody getAxiomBody(){
		return this.axiomBody ;
	}
	
	protected OMElement getPayloadElement() throws OMException {
		return getAxiomBody().getFirstElement();
	}
}
